// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package resource_dao

import (
	"context"
	"fmt"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/resource_card_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type CardStateItem struct {
	ObjectId     string                        `bson:"_id"`
	MemberUserId string                        `bson:"memberUserId"`
	ResourceId   string                        `bson:"resourceId"`
	CardId       string                        `bson:"cardId"`
	LearnLevel   resource_card_api.LEARN_LEVEL `bson:"learnLevel"`
	LearnTime    int64                         `bson:"learnTime"`
}

type _CardStateDao struct {
	mc *mongo.Collection
}

func newCardStateDao(db *mongo.Database) (*_CardStateDao, error) {
	mc := db.Collection(common.DB_NS_RESOURCE + "card_state")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"memberUserId", 1}, {"resourceId", 1}},
		},
		{
			Keys: bson.D{{"resourceId", 1}, {"cardId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_CardStateDao{mc: mc}, nil
}

func (db *_CardStateDao) genObjectId(memberUserId, resourceId, cardId string) string {
	return fmt.Sprintf("%s:%s:%s", memberUserId, resourceId, cardId)
}

func (db *_CardStateDao) Set(ctx context.Context, dbItem *CardStateItem) error {
	dbItem.ObjectId = db.genObjectId(dbItem.MemberUserId, dbItem.ResourceId, dbItem.CardId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.ObjectId}, bson.M{
		"$set": dbItem,
	}, options.UpdateOne().SetUpsert(true))
	return err
}

func (db *_CardStateDao) Exist(ctx context.Context, memberUserId, resourceId, cardId string) (bool, error) {
	objectId := db.genObjectId(memberUserId, resourceId, cardId)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": objectId})
	return count > 0, err
}

func (db *_CardStateDao) Get(ctx context.Context, memberUserId, resourceId, cardId string) (*CardStateItem, error) {
	objectId := db.genObjectId(memberUserId, resourceId, cardId)
	retItem := &CardStateItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": objectId}).Decode(retItem)
	return retItem, err
}

func (db *_CardStateDao) List(ctx context.Context, memberUserId, resourceId string, filterByLearnLevel bool, learnLevel resource_card_api.LEARN_LEVEL) ([]*CardStateItem, error) {
	query := bson.M{"memberUserId": memberUserId, "resourceId": resourceId}
	if filterByLearnLevel {
		query["learnLevel"] = learnLevel
	}

	cursor, err := db.mc.Find(ctx, query)
	if err != nil {
		return nil, err
	}
	retList := []*CardStateItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_CardStateDao) RemoveByCard(ctx context.Context, resourceId, cardId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"resourceId": resourceId, "cardId": cardId})
	return err
}

func (db *_CardStateDao) RemoveByResource(ctx context.Context, resourceId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"resourceId": resourceId})
	return err
}
